package com.ysd.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.ysd.entity.DaiYanTuan;
import com.ysd.util.DBManger;

public class DaiYanTuanDaoImpl {
	public List<DaiYanTuan> getDaiYanTuanList(){
		
		List<DaiYanTuan> dytList = new ArrayList<DaiYanTuan>();
		Connection conn = DBManger.getConnection();
		Statement st = null;
		ResultSet rs=null;
		 
		try {
			st = conn.createStatement();
			rs = st.executeQuery("select * from DaiYanTuanDui");
			while(rs.next()) {
				int dyId = rs.getInt("daiYanTuanId");
				String dyLoGo = rs.getString("daiYanLoGo");
				String dyName = rs.getString("daiYanRenName");
				String dyZhiWei = rs.getString("daiYanZhiWei");
				DaiYanTuan dyt = new DaiYanTuan(dyId, dyLoGo, dyName, dyZhiWei);
				
				dytList.add(dyt);
			}
			return dytList;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally {
			DBManger.closeAll(conn, st, rs);
		}
	}
	/**
	 * 根据Id删除代言信息表
	 * dyId 要删除的代言团Id
	 * @param 
	 * 受影响行数
	 * @return
	 */
	public int deleteDaiYanTuanId(int dyId){
		return DBManger.executeUpdate("delete  from daiyantuandui where daiYanTuanId="+dyId);
		
		
	}
	
	/**
	 * 受影响行数 添加
	 * 返回int
	 */
	
	public int addDYT(DaiYanTuan dy) {
		return DBManger.executeUpdate("insert into daiyantuandui values(null,'"+dy.getDaiYanLoGo()+"','"+dy.getDaiYanRenName()+"','"+dy.getDaiYanZhiWei()+"')");
	}
	
	/**
	 * 根据代言人名字查询 数据
	 * 返回List
	 */
	public List<DaiYanTuan> getDYTByName(String name){
		List<DaiYanTuan> dyList = new ArrayList<DaiYanTuan>();
		Connection conn = DBManger.getConnection();
		Statement st = null;
		ResultSet rs = null;
		try {
			st = conn.createStatement();
			rs = st.executeQuery("select * from daiyantuandui where daiYanRenName ='"+name+"'");
			while(rs.next()) {
				int dyId = rs.getInt("daiYanTuanId");
				
				String dyName =rs.getString("daiYanRenName");
				String dyZhiWei = rs.getString("daiYanZhiWei");
				DaiYanTuan dy = new DaiYanTuan(0, "", dyName, dyZhiWei);
				dyList.add(dy);
			}
			return dyList;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally {
			DBManger.closeAll(conn, st, rs);
		}
		
		
	}
	
	public DaiYanTuan getDYTById(int id){

		Connection conn = DBManger.getConnection();
		Statement st = null; 
		ResultSet rs = null;
		DaiYanTuan dy=null;
		try {
			st = conn.createStatement();
			rs = st.executeQuery("SELECT * from daiyantuandui where daiYanTuanId = "+id);
			while(rs.next()) {
				int dyTuanDuiId = rs.getInt("daiYanTuanId");
				String dyLoGo = rs.getString("daiYanLoGo");
				String dyRenName = rs.getString("daiYanRenName");
				String dyZhiWei = rs.getString("daiYanZhiWei");
				 dy = new DaiYanTuan(dyTuanDuiId, dyLoGo, dyRenName, dyZhiWei);	
				
			}
			
			
			return dy;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally {
			DBManger.closeAll(conn, st, rs);
		}
	}
	
	/**
	 * 修改代言团队分类
	 * 
	 */
	public int updateDYT(DaiYanTuan dy) {
		String sql=("UPDATE daiyantuandui set  daiYanLoGo='"+dy.getDaiYanLoGo()+"' , daiYanRenName ='"+dy.getDaiYanRenName()+"',daiYanZhiWei='"+dy.getDaiYanZhiWei()+"' where daiYanTuanId="+dy.getDaiYanTuanId()+" ");
		System.out.println(sql);
		return DBManger.executeUpdate(sql);
	}
	
	
}
